package com.app.couponsharelife.ui.main.login

import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import android.view.View
import com.app.couponsharelife.R
import com.app.couponsharelife.base.BaseActivity
import com.app.couponsharelife.databinding.ActivityLoginBinding
import com.app.couponsharelife.ui.adapter.ViewPagerAdapter
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity : BaseActivity() {
    override val viewLayoutId: Int get() = R.layout.activity_login

    private var mAdapter: ViewPagerAdapter? = null

    private var mTitles = arrayListOf<String>()

    private var mPagers = arrayListOf<Fragment>()

    override fun initViews() {
        initViewPager()
        initClickEvent()
    }

    private fun initClickEvent() {
        (mViewDataBinding as ActivityLoginBinding).listener = View.OnClickListener {
            when (it.id) {
                R.id.ivBack -> finish()
                R.id.tvLoginType -> {
                    mViewPager.currentItem = 0
                }
                R.id.tvRegisterType -> {
                    mViewPager.currentItem = 1
                }
            }
        }
    }

    private fun initViewPager() {
        with(mTitles) {
            add("登录")
            add("注册")
        }
        with(mPagers) {
            add(LoginFragment.newInstance())
            add(RegisterFragment.newInstance())
        }
        mAdapter = ViewPagerAdapter(supportFragmentManager, mPagers, mTitles)
        mViewPager.adapter = mAdapter
        mViewPager.setScroll(false)
        mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(p0: Int) {

            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
            }

            override fun onPageSelected(p0: Int) {
                if (p0 == 0) {
                    tvLoginType.alpha = 1f
                    tvLoginType.textSize = 21f
                    tvRegisterType.alpha = 0.8f
                    tvRegisterType.textSize = 18f
                } else {
                    tvLoginType.alpha = 0.8f
                    tvLoginType.textSize = 18f
                    tvRegisterType.alpha = 1f
                    tvRegisterType.textSize = 21f
                }
            }
        })
    }
}
